package com.metadata_system.common.utils;

import lombok.SneakyThrows;
import org.apache.commons.lang.math.RandomUtils;
import org.springframework.beans.factory.annotation.Value;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

public class SliderCaptchaUtils {

    @SneakyThrows
    public static Map<String, Object> genarateImage(String generateFilePath,String sliderCaptchaImagesDir) {
        try {
            String file = (String.format("%02d", RandomUtils.nextInt(9) + 1)) + ".png";
            String realImagePath =  sliderCaptchaImagesDir + "/" + file;

            // 1.创建背景图片+素材图片
            // 创建一个宽度为100像素，高度为100像素的透明图片
            BufferedImage bgImage = new BufferedImage(340, 251, BufferedImage.TYPE_INT_ARGB);

            // 获取图片的Graphics对象，用于绘制
            Graphics2D g2d = bgImage.createGraphics();

            // 设置背景透明
            bgImage = g2d.getDeviceConfiguration().createCompatibleImage(340, 251, Transparency.TRANSLUCENT);

            System.out.println("realImagePath: "+realImagePath);
            BufferedImage realImage = ImageIO.read(new File(realImagePath));
            // 画图片2到新图片上
            Graphics2D bgImageG2d = bgImage.createGraphics();
            bgImageG2d.drawImage(realImage, 0, 0, null);


            // 2.【扣除】移动区域图片
            // 设置裁剪区域的起始坐标和宽高

            // 总宽度340
            int croppedX = 100 + RandomUtils.nextInt(150);
            int croppedY = 40 + RandomUtils.nextInt(50);
            int croppedWidth = 50;
            int croppedHeight = 50;

            // 创建一个新的BufferedImage对象，用于存储裁剪后的图像
            BufferedImage croppedImage = new BufferedImage(croppedWidth, croppedHeight, BufferedImage.TYPE_INT_RGB);

            // 获取Graphics2D对象，用于绘制裁剪后的图像
            Graphics2D croppedG2d = croppedImage.createGraphics();

            // 设置剪裁图片背景
            croppedG2d.setColor(Color.WHITE);
            croppedG2d.fillRect(0, 0, 50, 50);


            // 绘制裁剪后的图像
            croppedG2d.drawImage(realImage.getSubimage(croppedX, croppedY, croppedWidth - 2, croppedHeight - 2), 1, 1, null);
            croppedG2d.dispose();

            // 将【抠出来的图片】合并到左下角
            bgImageG2d.drawImage(croppedImage, 5, 196, null);


            // 3.在原抠图位置增加遮罩图
            BufferedImage shadeImage = new BufferedImage(croppedWidth - 2, croppedHeight - 2, BufferedImage.TYPE_INT_ARGB);

            // 获取Graphics2D对象以便于绘制
            Graphics2D shadeG2d = shadeImage.createGraphics();

            // 绘制一个半透明的矩形
            Color semiTransparentRed = new Color(0, 0, 0, 180); // 半透明的红色，alpha值为128（0-255之间）
            shadeG2d.setColor(semiTransparentRed);
            shadeG2d.fillRect(0, 0, croppedWidth, croppedHeight);

            shadeG2d.dispose();
            // 将【抠出来的图片】的【遮罩图片】合并到原位置标记
            bgImageG2d.drawImage(shadeImage, croppedX, croppedY, null);


            bgImageG2d.dispose();
            // 保存新图片
            ImageIO.write(bgImage, "png", new File(generateFilePath));

//            System.out.println("croppedX: " + croppedX);
//            System.out.println("croppedY: " + croppedY);

            HashMap<String, Object> map = new HashMap<>();
            map.put("X", croppedX);
            map.put("Y", croppedY);
            return map;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }


}
